package taller2.model.graph;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import taller2.interfaces.Connection;
import taller2.interfaces.Graph;
import taller2.interfaces.Node;
import taller2.utils.Constants;

public class GraphNotDirectedImpl extends GraphImpl {
	private static final long serialVersionUID = 1L;

	public GraphNotDirectedImpl(){
		super();
	}
	
	// creates the graph from another graph
	public GraphNotDirectedImpl(Graph other) {
		super(other);
		isDirected = other.isDirected();
		eliminateBadConnections();
	}

	// for when the graph was created from a directed graph
	private void eliminateBadConnections(){
		for(Node node : nodes){
			for(Connection connection : node.getConnections()){
				Node otherNode = connection.getOtherNode();
				int weight = connection.getWeight();
				node.addConnection(otherNode,weight);
				otherNode.addConnection(node,weight);
			}
		}
	}
	
	// returns true if the graph is directed
	public boolean isDirected() {
		return false;
	}	
	
	// connects the first with the second Node
	public void ConnectNodes(int index1,int index2, int weight){
		deleteConnections(index1,index2);
		
		Node firstNode = nodes.get(index1);
		Node secondNode = nodes.get(index2);
		firstNode.addConnection(secondNode, weight);
		secondNode.addConnection(firstNode, weight);
	}
	
	// saves the graph to a file
	public void save(String path) {
		GraphSerializer graphSer = new GraphSerializer();
		graphSer.save(path, this);
	}

    // loads the graph from the file
	public void load(String path) {
		GraphSerializer graphSer = new GraphSerializer();
		graphSer.load(path, this);
	}
	
	class GraphSerializer{

    	public void save(String path, Graph graph) {
    		
    		try {
    			new File(Constants.savedGraphsPath).mkdirs();
    			FileOutputStream fileOut = new FileOutputStream(path);
    			ObjectOutputStream out = new ObjectOutputStream(fileOut);
    			out.writeObject( (GraphNotDirectedImpl) graph);
    	        out.close();
    	        fileOut.close();
    		} catch (IOException | NullPointerException e) {
    			e.printStackTrace();
    		}
    	}

    	public void load(String path, Graph graph) {
           
    		FileInputStream fileIn;
            
    		try {
    			fileIn = new FileInputStream(path);
    	        ObjectInputStream in = new ObjectInputStream(fileIn);
    	        Graph loadedGraph = (Graph) in.readObject();
    	        GraphNotDirectedImpl loadedGraphTyped = new GraphNotDirectedImpl(loadedGraph);
    	        
    	        in.close();
    	        fileIn.close();
    	        
    	        graph.replace(loadedGraphTyped);
    	        graph.setSaveAsDirected(loadedGraphTyped.savedAsDirected());
    		} catch (IOException | ClassNotFoundException | NullPointerException e) {
    			//e.printStackTrace();
    		}
    	}
    }
   
}
